#include "sky_box.h"
#include "..\clientinterfaces.h"
#include "..\Screen\tex_mgr.h"
#include "iltclient.h"
  
sky_box::sky_box(void):
m_wide(3000),
m_height(1200),
m_long(3000),
m_offset(100),
m_rate(1.0f)
{
	for (int i=0;i<5;++i)
	{
		m_texs[i]=0;
	}
	m_center.Init();
	m_center.x=0;
	m_center.z=0;
	m_center.y=100;
  
}

sky_box::~sky_box(void)
{
	for (int i=0;i<5;++i)
	{
		if (m_texs[i])
		{
			g_pLTCTexInterface->ReleaseTextureHandle(m_texs[i]);
			m_texs[i]=0;
		}
		if (m_texs1[i])
		{
			g_pLTCTexInterface->ReleaseTextureHandle(m_texs1[i]);
			m_texs1[i]=0;
		}
	}
}

void sky_box::render()
{


	g_pLTCDrawPrim->SetTransformType(DRAWPRIM_TRANSFORM_WORLD);
	g_pLTCDrawPrim->SetAlphaBlendMode(DRAWPRIM_BLEND_MOD_SRCALPHA);
	g_pLTCDrawPrim->SetFogEnable(true);
	

	

	for (int i=0;i<5;++i)
	{
		if (m_rate==1.0f)
		{
			g_pLTCDrawPrim->SetTexture(m_texs[i]);
			g_pLTCDrawPrim->DrawPrim(&m_polys[i]);
		}
		else if (m_rate==0.0f)
		{
			g_pLTCDrawPrim->SetTexture(m_texs1[i]);
			g_pLTCDrawPrim->DrawPrim(&m_polys_back[i]);
		}
		else
		{
			g_pLTCDrawPrim->SetTexture(m_texs1[i]);
			g_pLTCDrawPrim->DrawPrim(&m_polys_back[i]);
			g_pLTCDrawPrim->SetTexture(m_texs[i]);
			g_pLTCDrawPrim->DrawPrim(&m_polys[i]);
		}
	}
	g_pLTCDrawPrim->SetAlphaBlendMode(DRAWPRIM_BLEND_MOD_SRCALPHA);
	g_pLTCDrawPrim->SetFogEnable(false);
}

/******************************
verts[0]        verts[1]
_______________
|				    |
|     center			|
|_______________|
verts[3]		verts[2]
******************************/
#define BLUE_SKY_TEX_OFFSET 8
#define MORNING_SKY_TEX_OFFSET 13
void sky_box::init()
{
	//load texs
	for (int i=0;i<5;++i)
	{
		m_texs[i]=tex_mgr::single().get_tex(i+BLUE_SKY_TEX_OFFSET);
		m_texs1[i]=tex_mgr::single().get_tex(i+MORNING_SKY_TEX_OFFSET);
		m_polys[i].rgba.a=255*m_rate;
		m_polys_back[i].rgba.a=255*(1-m_rate);

		m_polys[i].rgba.r=m_polys[i].rgba.g=m_polys[i].rgba.b=255;
		m_polys_back[i].rgba.r=m_polys_back[i].rgba.g=m_polys_back[i].rgba.b=255;

	}
	reload_pos(m_polys,0.0f);
	reload_pos(m_polys_back,m_offset);



}

void sky_box::set_size( float w,float h,float l )
{
	m_wide=w;
	m_height=h;
	m_long=l;

}

void sky_box::set_center( LTVector pos )
{
	m_center=pos;

}

void sky_box::change_sky( float rate )
{
	
	m_rate-=rate;
	if (m_rate<=0.0f)
	{
		m_rate=0.0f;
	}

    for (int i=0;i<5;++i)
	{
		m_polys[i].rgba.a=255*m_rate;
		m_polys_back[i].rgba.a=255*(1-m_rate);
	}


}

void sky_box::reload_pos(LT_POLYFT4* _polys,float offset)
{
	float _wide=m_wide+offset;
	float _height=m_height+offset;
	float _long=m_long+offset;

	//front
	//----vert0
	_polys[0].verts[0].x=m_center.x-_wide/2.0f;
	_polys[0].verts[0].y=m_center.y+_height/2.0f;
	_polys[0].verts[0].z=m_center.z+_long/2.0f;
	_polys[0].verts[0].u=0.0f;
	_polys[0].verts[0].v=0.0f;
	//----vert1
	_polys[0].verts[1].x=m_center.x+_wide/2.0f;
	_polys[0].verts[1].y=m_center.y+_height/2.0f;
	_polys[0].verts[1].z=m_center.z+_long/2.0f;
	_polys[0].verts[1].u=1.0f;
	_polys[0].verts[1].v=0.0f;
	//----vert2
	_polys[0].verts[2].x=m_center.x+_wide/2.0f;
	_polys[0].verts[2].y=m_center.y-_height/2.0f;
	_polys[0].verts[2].z=m_center.z+_long/2.0f;
	_polys[0].verts[2].u=1.0f;
	_polys[0].verts[2].v=1.0f;
	//----vert3
	_polys[0].verts[3].x=m_center.x-_wide/2.0f;
	_polys[0].verts[3].y=m_center.y-_height/2.0f;
	_polys[0].verts[3].z=m_center.z+_long/2.0f;
	_polys[0].verts[3].u=0.0f;
	_polys[0].verts[3].v=1.0f;



	//right
	//----vert0
	_polys[1].verts[0].x=m_center.x-_wide/2.0f;
	_polys[1].verts[0].y=m_center.y+_height/2.0f;
	_polys[1].verts[0].z=m_center.z-_long/2.0f;
	_polys[1].verts[0].u=0.0f;
	_polys[1].verts[0].v=0.0f;
	//----vert1
	_polys[1].verts[1]=_polys[0].verts[0];
	_polys[1].verts[1].u=1.0f;
	_polys[1].verts[1].v=0.0f;
	//----vert2
	_polys[1].verts[2]=_polys[0].verts[3];
	_polys[1].verts[2].u=1.0f;
	_polys[1].verts[2].v=1.0f;
	//----vert3
	_polys[1].verts[3].x=m_center.x-_wide/2.0f;
	_polys[1].verts[3].y=m_center.y-_height/2.0f;
	_polys[1].verts[3].z=m_center.z-_long/2.0f;
	_polys[1].verts[3].u=0.0f;
	_polys[1].verts[3].v=1.0f;





	//back
	//----vert0
	_polys[2].verts[0].x=m_center.x+_wide/2.0f;
	_polys[2].verts[0].y=m_center.y+_height/2.0f;
	_polys[2].verts[0].z=m_center.z-_long/2.0f;
	_polys[2].verts[0].u=0.0f;
	_polys[2].verts[0].v=0.0f;
	//----vert1
	_polys[2].verts[1]=_polys[1].verts[0];
	_polys[2].verts[1].u=1.0f;
	_polys[2].verts[1].v=0.0f;
	//----vert2
	_polys[2].verts[2]=_polys[1].verts[3];
	_polys[2].verts[2].u=1.0f;
	_polys[2].verts[2].v=1.0f;
	//----vert3
	_polys[2].verts[3].x=m_center.x+_wide/2.0f;
	_polys[2].verts[3].y=m_center.y-_height/2.0f;
	_polys[2].verts[3].z=m_center.z-_long/2.0f;
	_polys[2].verts[3].u=0.0f;
	_polys[2].verts[3].v=1.0f;

	//left
	//----vert0
	_polys[3].verts[0]=_polys[0].verts[1];
	_polys[3].verts[0].u=0.0f;
	_polys[3].verts[0].v=0.0f;
	//----vert1
	_polys[3].verts[1]=_polys[2].verts[0];
	_polys[3].verts[1].u=1.0f;
	_polys[3].verts[1].v=0.0f;
	//----vert2
	_polys[3].verts[2]=_polys[2].verts[3];
	_polys[3].verts[2].u=1.0f;
	_polys[3].verts[2].v=1.0f;
	//----vert3
	_polys[3].verts[3]=_polys[0].verts[2];
	_polys[3].verts[3].u=0.0f;
	_polys[3].verts[3].v=1.0f;

	//up
	//----vert0
	_polys[4].verts[0]=_polys[0].verts[0];
	_polys[4].verts[0].u=0.0f;
	_polys[4].verts[0].v=1.0f;
	//----vert1
	_polys[4].verts[1]=_polys[0].verts[1];
	_polys[4].verts[1].u=1.0f;
	_polys[4].verts[1].v=1.0f;
	//----vert2
	_polys[4].verts[2]=_polys[2].verts[0];
	_polys[4].verts[2].u=1.0f;
	_polys[4].verts[2].v=0.0f;
	//----vert3
	_polys[4].verts[3]=_polys[2].verts[1];
	_polys[4].verts[3].u=0.0f;
	_polys[4].verts[3].v=0.0f;







}